package com.zgq.share.ui;

import com.zgq.share.tools.DialogTool;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * 自己实现WebView，供OAuth认证时打开Url 为什么要自己实现这样一个WebView？ 答：
 * 就是当用户认证是选择用UC、QQ浏览器第三方的浏览器进行用户认证时，当用户输入账号密码后点击授权按钮后不会跳转。
 * 只有用Android自带的浏览器才没有问题。但是大多数的用户都会用UC等第三方的浏览器了，这样导致认证不能正常进行。 所以需要自己实现一个WebView
 * 
 * @author
 */
public class WebViewActivity extends Activity {
	private WebView webView;
	private Intent intent = null;
	public static WebViewActivity webInstance = null;
	public final static int RESULT_CODE = 1;
	public static String VERIFIER;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web);

		webInstance = this;
		webView = (WebView) findViewById(R.id.web_show);
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSaveFormData(true);
		webSettings.setSavePassword(true);
		webSettings.setSupportZoom(true);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		// 设置触摸事件
		webView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				webView.requestFocus();
				return false;
			}
		});
		// 设置进度条
		webView.setWebChromeClient(new WebChromeClient() {

			public void onProgressChanged(WebView view, int progress) {

//				setTitle(getResources().getString(R.string.share_wait_tip)
//						+ progress + "%");
				setProgress(progress * 100);

				if (progress == 100) {
//					setTitle(R.string.app_name);
				}
			}
		});

		intent = this.getIntent();
		if (!intent.equals(null)) {
			Bundle b = intent.getExtras();
			// 新浪微博请求URL
			if (b != null && b.containsKey("uri")) {
//				setTitle(R.string.share_title);
				webView.loadUrl(b.getString("uri"));
				System.out.println("url=" + b.getString("uri"));
				webView.setWebChromeClient(new WebChromeClient() {

					public void onProgressChanged(WebView view, int progress) {

						if (progress == 100) {
							setTitle(R.string.app_name);
						}
					}
				});
			}

			// 腾讯微博的请求url
			/*if (b.containsKey("Tencenturl")) {
				setTitle(R.string.third_tencent_login);
				final String urlStr = b.getString("Tencenturl");
				webView.requestFocus();
				webView.loadUrl(urlStr);
				Log.i("WebView Starting", urlStr);

				WebViewClient client = new WebViewClient() {
					@Override
					public void onPageFinished(WebView view, String url) {
						Log.i("WebView onpagefinish", url);
						if (url.contains("v=")) {

							VERIFIER = url.substring(url.length() - 6);
							Log.e("verifier", "verifier:" + VERIFIER);
							Intent intent = new Intent();
							setResult(RESULT_CODE, intent);
							finish();
						}
						super.onPageFinished(view, url);
					}
				};
				webView.setWebViewClient(client);
			}*/
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
}